#include <my_header.h>

#include "registered.h"




int usrtable_add(const char *usrname,const char *encrypted_key,const char *salt,MYSQL *mysql)
{
    char sql[1024] = {0};
    printf("----------------------\n");
    printf("usrname = %s\n",usrname);
    printf("salt = %s\n",salt);
    printf("encrypted_key = %s\n",encrypted_key);
    sprintf(sql,"insert into usrtable (username,salt,encrypted_password,is_deleted) values ('%s','%s','%s','%d')",usrname,salt,encrypted_key,0);
    int qret = mysql_query(mysql,sql);
    if(qret != 0){

        fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
        printf("添加表项失败！\n");
        return -1;
    }
    printf("添加表项成功！\n");
    return 0;
}



int registered_server(const char *usrname,const char *password,MYSQL *mysql){
    char *salt = saltInit();
    printf("salt = %s\n",salt);
    char encrypted_key[128] = {0};
    strncpy(encrypted_key,crypt(password,salt),strlen(crypt(password,salt)));
    int ret = usrtable_add(usrname,encrypted_key,salt,mysql);
    if(ret == 0){
        char sql[1024] = {0};
        sprintf(sql,"insert into usrfile (filename,username,path,type) values ('/','%s','/','d')",usrname);
        int qret = mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"mysql_query:%s\n",mysql_error(mysql));
            printf("添加usrfile表项失败！\n");
            return -1;
        }
        printf("注册成功，已添加至用户表！\n");
        return 0;
    }
    else{
        printf("注册失败！\n");
        return -1;
    }
}

